/*****************************************************************************
 * plum_micro.h
 *
 * Copyright (C) 2020 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

#ifndef __PLUM_MICRO_H__
#define __PLUM_MICRO_H__
/*****************************************************************************
 * Included Files
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/* 符号x转字符串 */
#define PLUM_STRING(x)  PLUM_STRING_(x)
#define PLUM_STRING_(x) #x

/* 连接字符p1，p2 */
#define PLUM_CONTACT(p1, p2)  PLUM_COMTACT_(p1, p2)
#define PLUM_COMTACT_(p1, p2) p1##p2

/* 数组x成员个数 */
#define PLUM_ARRAY_CNT(x) (sizeof(x) / sizeof(x[0]))

/* 判断ptr是否是数组array成员 */
#define PLUM_PART_OF_ARRAY(array, ptr)                   \
    ((ptr) && ((void *)(ptr) >= (void *)&((array)[0]) && \
               (void *)(ptr) < (void *)&((array)[PLUM_ARRAY_CNT(array)])))

/**
 * @brief:  计算结构体成员在结构体中的偏移量
 *
 * @param:  type    -   结构体变量类型
 * @param:  member  -   结构体成员名称
 **/
#define PLUM_OFFSET_OF(type, member) (unsigned long)(&(((type *)0)->member))

/**
 * @brief:  根据结构体成员，求结构体指针
 *
 * @param:  ptr     -   结构体成员指针
 * @param:  type    -   结构体变量类型
 * @param:  member  -   结构体成员名称
 **/
#define PLUM_CONTAINER_OF(ptr, type, member) \
    ((type *)(((char *)(ptr)) - PLUM_OFFSET_OF(type, member)))

/* bit置位 */
#define PLUM_GET_BIT(x, bit)   (((x) >> (bit)) & 1)
#define PLUM_SET_BIT(x, bit)   ((x) | (1 << (bit)))
#define PLUM_CLEAR_BIT(x, bit) ~((x) & (1 << (bit)))
#define PLUM_TOG_BIT(x, bit)   ((x) ^ (1 << (bit)))

/* 未使用 */
#define PLUM_UNUSED(x) (void)(x)

/* 计算最大、最小值、绝对值 */
#define PLUM_MIN(x, y) (((x) < (y)) ? (x) : (y))
#define PLUM_MAX(x, y) (((x) < (y)) ? (y) : (x))
#define PLUM_ABS(x)    (((x) > 0) ? (x) : -(x))

/* 系统宏 */
#if defined(__CC_ARM) || defined(__GNUC__)
#define PLUM_LINE __LINE__
#define PLUM_FILE __FILE__
#define PLUM_TIME __TIME__
#define PLUM_DATE __DATE__
#define PLUM_FUNC __FUNCTION__
#elif defined(__ICCARM__)

#endif

/*****************************************************************************
 * Public Types
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/*****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

/****************************************************************************/
#ifdef __cplusplus
}
#endif
/****************************************************************************/
#endif /* __PLUM_MICRO_H__ */
/****************************************************************************/

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
